package com.xique.api.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xique.api.bean.domain.ParkingOrder;
import com.xique.api.bean.search.ParkingOrderSearch;
import com.xique.api.mapper.ParkingOrderMapper;
import com.xique.api.service.service.IParkingOrderService;
import com.xique.common.core.utils.DateUtils;
import com.xique.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author shd
 * @create 2021/11/9 15:08
 */
@Service
public class ParkingOrderServiceImpl implements IParkingOrderService {

    @Autowired
    private ParkingOrderMapper parkingOrderMapper;

    /**
     * 查询停车流水订单-单次停车唯一标识
     *
     * @param id 停车流水订单-单次停车唯一标识ID
     * @return 停车流水订单-单次停车唯一标识
     */
    @Override
    public ParkingOrder selectParkingOrderById(Long id)
    {
        return parkingOrderMapper.selectById(id);
    }

    @Override
    public ParkingOrder selectParkingOrder(ParkingOrderSearch search) {
        QueryWrapper<ParkingOrder> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(search.getParkingSerial())) {
            wrapper.eq("parking_serial", search.getParkingSerial());
        }
        wrapper.orderByDesc("UNIX_TIMESTAMP(update_time)")
                .last("limit 1");
        return parkingOrderMapper.selectOne(wrapper);
    }

    /**
     * 查询停车流水订单-单次停车唯一标识列表
     *
     * @param search 停车流水订单-单次停车唯一标识
     * @return 停车流水订单-单次停车唯一标识
     */
    @Override
    public IPage<ParkingOrder> selectParkingOrderPage(ParkingOrderSearch search)
    {
        QueryWrapper<ParkingOrder> wrapper = new QueryWrapper<>();
        if (null != search.getParkId()) {
            wrapper.eq("park_id", search.getParkId());
        }
        if (StringUtils.isNotEmpty(search.getPlate())) {
            wrapper.eq("plate", search.getPlate());
        }
        if (null != search.getStartTime()) {
            wrapper.ge("update_time", search.getStartTime());
        }
        if (null != search.getEndTime()) {
            wrapper.le("update_time", search.getEndTime());
        }
        Page<ParkingOrder> page = new Page<>();
        if (null != search.getPageNum() && null != search.getPageSize()) {
            page.setCurrent(search.getPageNum());
            page.setSize(search.getPageSize());
        }
        wrapper.orderByDesc("update_time");
        return parkingOrderMapper.selectPage(page, wrapper);
    }

    /**
     * 新增停车流水订单-单次停车唯一标识
     *
     * @param parkingOrder 停车流水订单-单次停车唯一标识
     * @return 结果
     */
    @Override
    public int insertParkingOrder(ParkingOrder parkingOrder)
    {
        parkingOrder.setCreateTime(DateUtils.getNowDate());
        return parkingOrderMapper.insert(parkingOrder);
    }

    /**
     * 修改停车流水订单-单次停车唯一标识
     *
     * @param parkingOrder 停车流水订单-单次停车唯一标识
     * @return 结果
     */
    @Override
    public int updateParkingOrder(ParkingOrder parkingOrder)
    {
        parkingOrder.setUpdateTime(DateUtils.getNowDate());
        return parkingOrderMapper.updateById(parkingOrder);
    }

    /**
     * 删除停车流水订单-单次停车唯一标识对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteParkingOrderByIds(List<Long> ids)
    {
        return parkingOrderMapper.deleteBatchIds(ids);
    }

    /**
     * 删除停车流水订单-单次停车唯一标识信息
     *
     * @param id 停车流水订单-单次停车唯一标识ID
     * @return 结果
     */
    @Override
    public int deleteParkingOrderById(Long id)
    {
        return parkingOrderMapper.deleteById(id);
    }
}
